package ru.len.cafe.ejb.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import ru.len.cafe.entity.model.Dish;
import ru.len.cafe.entity.model.Serve;

/**
 * 
 * @author Stanislav Chetvetkov
 *
 */

@Stateless
public class DishDao {

    @PersistenceContext(unitName="LenCafePU")
    EntityManager em;
    
    public List<Dish> getMenu(){
	Query query = em.createQuery("select g from Dish g");
	@SuppressWarnings("unchecked")
	List<Dish> resultList = (List<Dish>) query.getResultList();
	return resultList;
    }
    
    /**
     * Adds new dish to the database
     * @param dish
     */
    public boolean createDish(Dish dish){
	if (!em.contains(dish)){
	    em.persist(dish);
	    return true;
	}else{
	    return false;
	}

    }
    
    /**
     * 
     * @param dish
     */
    public void removeDish(Dish dish){
	em.remove(dish);
    }
    
    /**
     * 
     * @param dish
     */
    public void updateDish(Dish dish){
	em.refresh(dish);
    }
    
    public Dish findDishById(Long dishId){
	return ((Dish) em.find(Dish.class, dishId));
    }
    
}
